package sxe.core.math;

import snobject.snDouble;
import snobject.snObject;
import snobject.snObjectArgList;
import step.FunctionWrapper;
import step.EvaluationFailure;
import sxe.core.EvaluationFailureReasons;

/**
 * Returns the correctly rounded positive square root of a value.
 * @param argv must contain an snDouble object at top of the stack
 * @return returns the snDouble value of the positive square root of a
 */

public class sqrt extends FunctionWrapper{
	
	public snObject Call(snObjectArgList argv) throws EvaluationFailure{
		// REQUIRES: argv must contain snInteger or snDouble objects
		// EFFECTS: if trying to take the square of a negative value, throw EvaluationFailure exception 
		//			otherwise returns the snDouble of the positive square root of a
		double ret = argv.popDouble().getDbl();
		
		if(ret < 0)
			throw new EvaluationFailure(super.expnode,
					EvaluationFailureReasons.NEGATIVE_SQUARE_ROOT);
		
		return new snDouble(Math.sqrt(ret));
	}
}